package com.stardata.liangshisq.tcslmon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stardata.liangshisq.tcslmon.annotation.IgnoreAuth;
import com.stardata.liangshisq.tcslmon.annotation.LoginUser;
import com.stardata.liangshisq.tcslmon.entity.ArchArea;
import com.stardata.liangshisq.tcslmon.entity.DbiEmp;
import com.stardata.liangshisq.tcslmon.entity.OrderVo;
import com.stardata.liangshisq.tcslmon.entity.ProductVo;
import com.stardata.liangshisq.tcslmon.mapper.ArchAreaMapper;
import com.stardata.liangshisq.tcslmon.service.BizBsService;
import com.stardata.liangshisq.tcslmon.service.KcKscService;
import com.stardata.liangshisq.tcslmon.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Samson Shu
 * @email shush@stardata.top
 * @date 2020/8/8 19:13
 */
@Api(tags = "旧版后厨界面接口")
@RestController
@AllArgsConstructor
@RequestMapping("/v1/kitchen/{shopId}")
public class KitchenControllerV1 extends ApiBaseAction {

  private final ArchAreaMapper areaMapper;
  private final KcKscService kscService;
  private final BizBsService bsService;

  @GetMapping("/areas")
  @ApiOperation(value = "查询餐厅所有的就餐区域列表")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  @IgnoreAuth
  public Object areas(@PathVariable String shopId) {
    QueryWrapper<ArchArea> wrapper = new QueryWrapper<>();
    wrapper.eq("belong_shop_id", Long.valueOf(shopId));
    List<ArchArea> list = areaMapper.selectList(wrapper);
    return successList(list);
  }

  @GetMapping("/orders")
  @ApiOperation(value = "查询后厨终端所需的订单信息")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  @IgnoreAuth
  public Object queryTableInfosForKitchen(@PathVariable String shopId, Integer status, String areas, String orderby) {

    List<Long> areaList = new ArrayList<>();
    if (!StringUtils.isBlank(areas)) {
      String[] arr = areas.split(",");
      for (String s : arr) {
        areaList.add(Long.parseLong(s));
      }
    }
    List<Long> orderIds = kscService.queryOrderIds(Long.parseLong(shopId), status, areaList, orderby);
    List<OrderVo> orders = bsService.getOrderVosByIds(orderIds, status, true,
        true, true);

    return successList(orders);
  }

  @GetMapping("/items4cancel")
  @ApiOperation(value = "查询后厨终端可取消的菜品信息")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  public Object queryCancelableItems(@PathVariable String shopId, Integer status, String areas) {

    List<Long> areaList = new ArrayList<>();
    if (!StringUtils.isBlank(areas)) {
      String[] arr = areas.split(",");
      for (String s : arr) {
        areaList.add(Long.parseLong(s));
      }
    }
    List<ProductVo> products = kscService.queryCancelableItems(Long.parseLong(shopId), status, areaList);

    return successList(products);
  }


  @PutMapping("/preparation")
  @ApiOperation(value = "完成指定ID列表的后厨配菜操作")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  public Object prepareItem(@LoginUser DbiEmp user, @PathVariable String shopId, String kscids) {
    if (StringUtils.isBlank(kscids)) {
      return fail(HttpStatus.BAD_REQUEST,"Must provide kscIds");
    }

    List<Long> kscIdList = new ArrayList<>();
    String[] arr = kscids.split(",");
    for (String s : arr) {
      kscIdList.add(Long.parseLong(s));
    }
    kscService.prepareByList(user.getId(), kscIdList);
    return success();
  }

  @PutMapping("/preparation_cancel")
  @ApiOperation(value = "完成指定ID列表的后厨配菜取消操作")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  public Object cancelPrepareItem(@LoginUser DbiEmp user, String kscids) {
    if (StringUtils.isBlank(kscids)) {
      return fail(HttpStatus.BAD_REQUEST,"Must provide kscids");
    }

    List<Long> kscIdList = new ArrayList<>();
    String[] arr = kscids.split(",");
    for (String s : arr) {
      kscIdList.add(Long.parseLong(s));
    }
    kscService.cancelPreparationByList(user.getId(), kscIdList);
    return success();
  }

  @PutMapping("/passon")
  @ApiOperation(value = "完成指定ID列表的后厨传菜操作")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  public Object passonItem(@LoginUser DbiEmp user, String kscids) {
    if (StringUtils.isBlank(kscids)) {
      return fail(HttpStatus.BAD_REQUEST, "Must provide kscids");
    }

    List<Long> kscIdList = new ArrayList<>();
    String[] arr = kscids.split(",");
    for (String s : arr) {
      kscIdList.add(Long.parseLong(s));
    }
    kscService.passonByList(user.getId(), kscIdList);
    return success();
  }

  @PutMapping("/passon_cancel")
  @ApiOperation(value = "完成指定ID列表的后厨传菜取消操作")
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "header", name="X-Liangshisq-Token", value="鉴权token", required = true),
  })
  public Object cancelPassonItem(@LoginUser DbiEmp user, String kscids) {
    if (StringUtils.isBlank(kscids)) {
      return fail(HttpStatus.BAD_REQUEST,"Must provide kscids");
    }

    List<Long> kscIdList = new ArrayList<>();
    String[] arr = kscids.split(",");
    for (String s : arr) {
      kscIdList.add(Long.parseLong(s));
    }
    kscService.cancelPassonByList(user.getId(), kscIdList);
    return success();
  }
}
